//
// Created by xuchao on 16-11-21.
//

#include "OkPgPreparedStatement.h"
#include <stdio.h>

using namespace OKPg;

int OkPgPreparedStatement::stmt_ = -1;

OKPg::OkPgPreparedStatement::OkPgPreparedStatement(const int paramCount)
        : paramCount_(paramCount){
    ++stmt_;
    stmtName_ = toString(stmt_);
}

OKPg::OkPgPreparedStatement::~OkPgPreparedStatement() {

}

std::string OKPg::OkPgPreparedStatement::getStmt() const {
    return toString<int>(stmt_);
}

void OKPg::OkPgPreparedStatement::setInt(const int paramIndex, const int value) {
    const int i = checkParamIndex(paramIndex, paramCount_);
    snprintf(param[i].v, 64, "%d", value);
    paramValues_[i] = param[i].v;
    paramLengths_[i] = 0;
    paramFormats_[i] = 0;
}

void OKPg::OkPgPreparedStatement::setString(const int paramIndex, const std::string &value) {
    const int i = checkParamIndex(paramIndex, paramCount_);
    paramValues_[i] = value;
    paramLengths_[i] = 0;
    paramFormats_[i] = 0;

}

void OKPg::OkPgPreparedStatement::setDouble(const int paramIndex, const double value) {
    const int i = checkParamIndex(paramIndex, paramCount_);
    snprintf(param[i].v, 64, "%lf", value);
    paramValues_[i] = param[i].v;
    paramLengths_[i] = 0;
    paramFormats_[i] = 0;
}

void OKPg::OkPgPreparedStatement::setLongLong(const int paramIndex, const long long value) {
    const int i = checkParamIndex(paramIndex, paramCount_);
    snprintf(param[i].v, 64, "%lld", value);
    paramValues_[i] = param[i].v;
    paramLengths_[i] = 0;
    paramFormats_[i] = 0;
}

void OKPg::OkPgPreparedStatement::setTimestamp(const int paramIndex, const time_t &value) {
    const int i = checkParamIndex(paramIndex, paramCount_);
    paramValues_[i] = time2string(value);
    paramLengths_[i] = 0;
    paramFormats_[i] = 0;
}

